//
//  afn_api.m
//  ziLu_ios
//
//  Created by mac on 16/3/17.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "afn_api.h"

@implementation afn_api
static AFHTTPRequestOperationManager *manager;
static AFHTTPSessionManager *man;
+(void)MyApi:(NSDictionary *)dic url:(NSString *)url success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))fail{
    //NSMutableDictionary *params = [self getBaseParams];
    //[params addEntriesFromDictionary:dic];
    //  NSLog(@"字典的内容%@",params);
    [self  wzx_post:url
              parameters:dic
                 success:success
                 failure:fail];
    
}
//添加请求通用参数
+ (NSMutableDictionary *)getBaseParams{
    //获取版本号
    NSString *sevr = [NSString stringWithFormat:@"%f",[[[UIDevice currentDevice] systemVersion] floatValue]];
    //唯一标识符
    NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    //获取app版本号
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    //获取
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [params setObject:@"2" forKey:@"os"];//安卓:1;iOS: 2;WinPhone: 3
    [params setObject:sevr forKey:@"sver"];//操作系统版本号
    [params setObject:app_Version forKey:@"ver"];      //app版本号
    [params setObject:identifierStr forKey:@"gid"];//设备唯一标示（推送也要用到这个来区分用户）
    [params setObject:@"App Store" forKey:@"channel"];//APP下载的渠道标识
    [params setObject:@"" forKey:@"ua"];        //user-agent
    [params setObject:@"2" forKey:@"plat"];

    return params;
}
//封装post函数
+ (void)wzx_post:(NSString *)URLString
      parameters:(id)parameters
         success:(void (^)(id responseObject))success
         failure:(void (^)(NSError *error))fail
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    
//    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
//    securityPolicy.allowInvalidCertificates = YES;
//    securityPolicy.validatesDomainName = NO;
//    manager.securityPolicy = securityPolicy;
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/html",@"text/json",@"text/plain",nil];
    manager.securityPolicy.allowInvalidCertificates = YES;
 
    
    [manager POST:URLString parameters:parameters success:^void(AFHTTPRequestOperation * operation, id responseObject) {
        
        
        
        long status = [responseObject[@"status"]integerValue];
        if (status == 200) { //返回成功
            
        } else {
            if (status == 99999) {
                
                UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"您的密码已修改" message:@"请重新登录"
                                                                delegate:self
                                                       cancelButtonTitle:nil
                                                       otherButtonTitles:@"确定", nil];
                
                NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
                [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
                
                [alertView show];
            }
            
            fail(responseObject);//返回失败原因
        }
        
        success(responseObject);
    } failure:^void(AFHTTPRequestOperation * operation, NSError * error) {
        fail(error);//返回失败原因
    }];
}


+(void)MyPhotoApi:(NSDictionary *)dic url:(NSString *)url photo:(NSData*)data  success:(void (^)(NSURLSessionDataTask *task, id responseObject))success  failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{


    [self hu_hao:dic url:url photo:data success:success failure:failure];
}


+(void)hu_hao:(NSDictionary *)dic url:(NSString *)url  photo:(NSData*)data  success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    
    man.responseSerializer = [AFHTTPResponseSerializer serializer];
    man.securityPolicy.allowInvalidCertificates = YES;
    
    [man POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //获取当前时间
        NSDate *now = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
        
        NSInteger year = [dateComponent year];
        NSInteger month = [dateComponent month];
        NSInteger day = [dateComponent day];
        NSInteger hour = [dateComponent hour];
        NSInteger minute = [dateComponent minute];
        NSInteger second = [dateComponent second];
        
        
        NSString*str=[NSString stringWithFormat:@"%ld_%ld_%ld_%ld:%ld:%ld.png",year,month,day,(long)hour,(long)minute,(long)second];
        NSLog(@"当前时间：%@",str);
        

        
        [formData appendPartWithFileData:data name:@"file" fileName:str mimeType:@"image/png"];
    } success:success failure:failure];

}





@end
